﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings
using System;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.UI.ViewModel.Controller;
#endregion

namespace ScrumTable.UI.ViewModel.Test
{
    /// <summary>
    /// Tests the ScrumController
    /// </summary>
    [TestClass]
    public class ScrumControllerTest
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }
        public IUnityContainer Container { get; set; }
        #endregion


        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        
        public ScrumControllerTest()
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterInstance<IUnityContainer>(container);
            ScrumController scrumController = container.Resolve<ScrumController>();
            container.RegisterInstance(scrumController);
            Container = container;
        }
        #endregion



        #region Additional test attributes
        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        
        [TestMethod]
        public void ScrumControllerFireEvent()
        {
            foreach (NavigationTarget x in Enum.GetValues(typeof( NavigationTarget)))
            {
                
                    ScrumControllerFireEvent(x, x != NavigationTarget.Null);
               
            }
        }

        [TestMethod]
        public void ScrumControllerViewModel()
        {
            bool hasFired = false;
            ChangedEventHandler changedEventHandler = (sender, e) => { hasFired = e.Target == NavigationTarget.StartView; };
            Container.Resolve<ScrumController>().ChangedView += changedEventHandler;
            Container.Resolve<ViewModelBase>().CurrentTarget = NavigationTarget.StartView;
            Container.Resolve<ScrumController>().ChangedView -= changedEventHandler;
            Assert.IsTrue(hasFired);
        }

        private void ScrumControllerFireEvent(NavigationTarget target, bool mustFire)
        {
            bool hasFired = false;
            ChangedEventHandler changedEventHandler = (sender, e) => { hasFired = e.Target == target; };
            Container.Resolve<ScrumController>().ChangedView += changedEventHandler;
            Container.Resolve<ScrumController>().GoTo(target);
            Container.Resolve<ScrumController>().ChangedView -= changedEventHandler;
            Assert.AreEqual(mustFire,hasFired);
        }
        #endregion
    }
}
